﻿using Halcon二次开发.Model;
using Halcon二次开发.Model.储放所有单例.储存每个单例界面;
using Halcon二次开发.Model.用户属性;
using Halcon二次开发.Tools;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Shapes;

namespace Halcon二次开发.View.登录界面
{
    public partial class EnterUrseForm : Form
    {
        public static EnterUrseForm urseForm;
        public static Form1 form1;
        public EnterUrseForm()
        {
            InitializeComponent();
            label3.Text = "";
            //生成文件
            if (!Directory.Exists($@"Data\账户信息"))
            {
                Directory.CreateDirectory($@"Data\账户信息");
            }
            if (!File.Exists($@"Data\账户信息\信息.json"))
            {
                File.Create($@"Data\账户信息\信息.json").Close();
            }
        }

        public static EnterUrseForm GetSingle(Form1 form) {
            form1 = form;
            if (urseForm==null)
            {
                urseForm = new EnterUrseForm();
            }
            return urseForm;
        }

        private void EnterUrseForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
        }

        /// <summary>
        /// 登录按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton1_Click(object sender, EventArgs e)
        {
            if (StaicResouce.UrseInforMationDb.IndexOf(new UrseInformation(ID.Text, PassWord.Text)) != -1)
            {
                label3.Text = "登录成功!";
                StaicResouce.UrseIndex = StaicResouce.UrseInforMationDb.IndexOf(new UrseInformation(ID.Text, PassWord.Text));
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText($"用户: {ID.Text}-登录成功! \r\n");
                if (StaicResouce.UrseIndex!=-1)
                {
                    switch (StaicResouce.UrseInforMationDb[StaicResouce.UrseIndex].Post)
                    {
                        case "超级用户":
                            form1.超级用户();
                            File.WriteAllText($@"Data\账户信息\登录用户.txt",StaicResouce.UrseIndex+"");
                            break;
                        case "管理者":
                            form1.管理员();
                            File.WriteAllText($@"Data\账户信息\登录用户.txt", StaicResouce.UrseIndex + "");
                            break;
                        case "操作者":
                            form1.操作者();
                            File.WriteAllText($@"Data\账户信息\登录用户.txt", StaicResouce.UrseIndex + "");
                            break;
                    }
                    this.Hide();
                }
            }
            else { label3.Text = "账号密码错误!"; (FormDb.Db["Form1"] as Form1).textBox1.AppendText($"用户: {ID.Text}-登录失败! \r\n"); }
        }

        /// <summary>
        /// 注销按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton2_Click(object sender, EventArgs e)
        {
            label3.Text = "";
            ID.Text = "";
            PassWord.Text = "";
            StaicResouce.UrseIndex = -1;

        }
    }
}
